/*   1:    */ package com.atomikos.icatch.imp;
/*   2:    */ 
/*   3:    */ import com.atomikos.icatch.HeurCommitException;
/*   4:    */ import com.atomikos.icatch.HeurHazardException;
/*   5:    */ import com.atomikos.icatch.HeurMixedException;
/*   6:    */ import com.atomikos.icatch.HeurRollbackException;
/*   7:    */ import com.atomikos.icatch.HeuristicMessage;
/*   8:    */ import com.atomikos.icatch.Participant;
/*   9:    */ import com.atomikos.icatch.RollbackException;
/*  10:    */ import com.atomikos.icatch.SysException;
/*  11:    */ import com.atomikos.icatch.TxState;
/*  12:    */ import com.atomikos.icatch.imp.thread.InterruptedExceptionHelper;
/*  13:    */ import com.atomikos.icatch.system.Configuration;
/*  14:    */ import java.util.Dictionary;
/*  15:    */ import java.util.Enumeration;
/*  16:    */ import java.util.Stack;
/*  17:    */ import java.util.Vector;
/*  18:    */ 
/*  19:    */ class ActiveStateHandler
/*  20:    */   extends CoordinatorStateHandler
/*  21:    */ {
/*  22:    */   private long rollbackTicks_;
/*  23:    */   private int globalSiblingCount_;
/*  24:    */   
/*  25:    */   ActiveStateHandler(CoordinatorImp coordinator)
/*  26:    */   {
/*  27: 61 */     super(coordinator);
/*  28: 62 */     this.rollbackTicks_ = 0L;
/*  29:    */   }
/*  30:    */   
/*  31:    */   protected long getRollbackTicks()
/*  32:    */   {
/*  33: 73 */     return this.rollbackTicks_;
/*  34:    */   }
/*  35:    */   
/*  36:    */   protected Object getState()
/*  37:    */   {
/*  38: 78 */     return TxState.ACTIVE;
/*  39:    */   }
/*  40:    */   
/*  41:    */   protected void onTimeout()
/*  42:    */   {
/*  43:    */     try
/*  44:    */     {
/*  45: 85 */       if (this.rollbackTicks_ < getCoordinator().getMaxRollbackTicks())
/*  46:    */       {
/*  47: 86 */         this.rollbackTicks_ += 1L;
/*  48:    */       }
/*  49: 91 */       else if (getCoordinator().getState().equals(getState()))
/*  50:    */       {
/*  51: 93 */         printMsg("Rollback of timedout ACTIVE coordinator !");
/*  52: 94 */         boolean indoubt = getCoordinator().isRecoverableWhileActive().booleanValue();
/*  53:    */         
/*  54:    */ 
/*  55: 97 */         super.rollback(indoubt, false);
/*  56:    */       }
/*  57:    */     }
/*  58:    */     catch (Exception e)
/*  59:    */     {
/*  60:101 */       printMsg("Error in timeout of ACTIVE state: " + e.getMessage() + " for coordinator " + getCoordinator().getCoordinatorId());
/*  61:    */     }
/*  62:    */   }
/*  63:    */   
/*  64:    */   protected void setGlobalSiblingCount(int count)
/*  65:    */   {
/*  66:108 */     this.globalSiblingCount_ = count;
/*  67:    */   }
/*  68:    */   
/*  69:    */   protected int prepare()
/*  70:    */     throws RollbackException, IllegalStateException, HeurHazardException, HeurMixedException, SysException
/*  71:    */   {
/*  72:116 */     Stack errors = new Stack();
/*  73:117 */     int count = 0;
/*  74:118 */     PrepareResult result = null;
/*  75:119 */     boolean allReadOnly = true;
/*  76:120 */     int ret = 0;
/*  77:121 */     Vector participants = getCoordinator().getParticipants();
/*  78:122 */     CoordinatorStateHandler nextStateHandler = null;
/*  79:124 */     if ((getCoordinator().checkSiblings()) && (this.globalSiblingCount_ != getCoordinator().getLocalSiblingCount()))
/*  80:    */     {
/*  81:    */       try
/*  82:    */       {
/*  83:129 */         if (Configuration.isDebugLoggingEnabled()) {
/*  84:129 */           Configuration.logDebug("Orphans detected: " + getCoordinator().getLocalSiblingCount() + " vs " + this.globalSiblingCount_ + " - forcing rollback.");
/*  85:    */         }
/*  86:132 */         super.rollback(getCoordinator().isRecoverableWhileActive().booleanValue(), false);
/*  87:    */       }
/*  88:    */       catch (HeurCommitException hc)
/*  89:    */       {
/*  90:135 */         throw new HeurMixedException(hc.getHeuristicMessages());
/*  91:    */       }
/*  92:138 */       throw new RollbackException("Orphans detected.");
/*  93:    */     }
/*  94:    */     try
/*  95:    */     {
/*  96:    */       try
/*  97:    */       {
/*  98:143 */         getCoordinator().setState(TxState.PREPARING);
/*  99:    */       }
/* 100:    */       catch (RuntimeException error)
/* 101:    */       {
/* 102:146 */         String msg = "Error in preparing: " + error.getMessage() + " - rolling back instead";
/* 103:147 */         Configuration.logWarning(msg, error);
/* 104:    */         try
/* 105:    */         {
/* 106:149 */           super.rollback(getCoordinator().isRecoverableWhileActive().booleanValue(), false);
/* 107:150 */           throw new RollbackException(msg);
/* 108:    */         }
/* 109:    */         catch (HeurCommitException e)
/* 110:    */         {
/* 111:152 */           Configuration.logWarning("Illegal heuristic commit during rollback before prepare:" + e);
/* 112:153 */           throw new HeurMixedException(e.getHeuristicMessages());
/* 113:    */         }
/* 114:    */       }
/* 115:156 */       count = participants.size();
/* 116:157 */       result = new PrepareResult(count);
/* 117:158 */       Enumeration enumm = participants.elements();
/* 118:159 */       while (enumm.hasMoreElements())
/* 119:    */       {
/* 120:160 */         Participant p = (Participant)enumm.nextElement();
/* 121:161 */         PrepareMessage pm = new PrepareMessage(p, result);
/* 122:162 */         if ((getCascadeList() != null) && (p.getURI() != null))
/* 123:    */         {
/* 124:166 */           Integer sibnum = (Integer)getCascadeList().get(p.getURI());
/* 125:168 */           if (sibnum != null) {
/* 126:169 */             p.setGlobalSiblingCount(sibnum.intValue());
/* 127:    */           }
/* 128:170 */           p.setCascadeList(getCascadeList());
/* 129:    */         }
/* 130:173 */         getPropagator().submitPropagationMessage(pm);
/* 131:    */       }
/* 132:178 */       result.waitForReplies();
/* 133:    */       
/* 134:180 */       boolean voteOK = result.allYes();
/* 135:181 */       setReadOnlyTable(result.getReadOnlyTable());
/* 136:    */       
/* 137:183 */       allReadOnly = result.allReadOnly();
/* 138:185 */       if (!voteOK)
/* 139:    */       {
/* 140:188 */         int res = result.getResult();
/* 141:    */         try
/* 142:    */         {
/* 143:218 */           rollback(true, false);
/* 144:    */         }
/* 145:    */         catch (HeurCommitException hc)
/* 146:    */         {
/* 147:227 */           errors.push(hc);
/* 148:228 */           throw new SysException("Unexpected heuristic: " + hc.getMessage(), errors);
/* 149:    */         }
/* 150:231 */         throw new RollbackException("Prepare: NO vote");
/* 151:    */       }
/* 152:    */     }
/* 153:    */     catch (RuntimeException runerr)
/* 154:    */     {
/* 155:236 */       errors.push(runerr);
/* 156:237 */       throw new SysException("Error in prepare: " + runerr.getMessage(), errors);
/* 157:    */     }
/* 158:    */     catch (InterruptedException err)
/* 159:    */     {
/* 160:241 */       InterruptedExceptionHelper.handleInterruptedException(err);
/* 161:242 */       errors.push(err);
/* 162:243 */       throw new SysException("Error in prepare: " + err.getMessage(), errors);
/* 163:    */     }
/* 164:247 */     if (allReadOnly)
/* 165:    */     {
/* 166:248 */       nextStateHandler = new TerminatedStateHandler(this);
/* 167:249 */       getCoordinator().setStateHandler(nextStateHandler);
/* 168:250 */       ret = 0;
/* 169:    */     }
/* 170:    */     else
/* 171:    */     {
/* 172:253 */       nextStateHandler = new IndoubtStateHandler(this);
/* 173:254 */       getCoordinator().setStateHandler(nextStateHandler);
/* 174:255 */       ret = 1;
/* 175:    */     }
/* 176:259 */     return ret;
/* 177:    */   }
/* 178:    */   
/* 179:    */   protected HeuristicMessage[] commit(boolean onePhase)
/* 180:    */     throws HeurRollbackException, HeurMixedException, HeurHazardException, IllegalStateException, RollbackException, SysException
/* 181:    */   {
/* 182:267 */     HeuristicMessage[] result = new HeuristicMessage[0];
/* 183:268 */     if (!onePhase) {
/* 184:269 */       throw new IllegalStateException("Illegal state for commit: ACTIVE!");
/* 185:    */     }
/* 186:272 */     if (getCoordinator().getParticipants().size() > 1)
/* 187:    */     {
/* 188:273 */       int prepareResult = 1;
/* 189:    */       
/* 190:    */ 
/* 191:    */ 
/* 192:    */ 
/* 193:    */ 
/* 194:    */ 
/* 195:    */ 
/* 196:281 */       setGlobalSiblingCount(1);
/* 197:282 */       prepareResult = prepare();
/* 198:284 */       if (prepareResult == 0) {
/* 199:285 */         result = getHeuristicMessages();
/* 200:    */       } else {
/* 201:287 */         result = commit(false, false);
/* 202:    */       }
/* 203:    */     }
/* 204:    */     else
/* 205:    */     {
/* 206:289 */       result = commit(false, true);
/* 207:    */     }
/* 208:291 */     return result;
/* 209:    */   }
/* 210:    */   
/* 211:    */   protected HeuristicMessage[] rollback()
/* 212:    */     throws HeurCommitException, HeurMixedException, SysException, HeurHazardException, IllegalStateException
/* 213:    */   {
/* 214:300 */     return rollback(getCoordinator().isRecoverableWhileActive().booleanValue(), false);
/* 215:    */   }
/* 216:    */   
/* 217:    */   protected Boolean replayCompletion(Participant participant)
/* 218:    */     throws IllegalStateException
/* 219:    */   {
/* 220:307 */     throw new IllegalStateException("No prepares sent yet.");
/* 221:    */   }
/* 222:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.imp.ActiveStateHandler
 * JD-Core Version:    0.7.0.1
 */